
//
//  BinaryTree_attach.swift
//  SwiftyDSA
//
//  Created by Archer on 2018/8/16.
//  Copyright © 2018年 Archer. All rights reserved.
//

import Foundation

extension BinaryTree {
    
    /// 将subtree作为node的左子树接入
    public func attachAsLC(from node: Node<Element>, subtree: BinaryTree<Element>) -> Node<Element> {
        node.lchild = subtree.root
        subtree.root?.parent = node.lchild
        _size += subtree._size
        updateHeightAbove(node)
        return node
    }
    
    /// 将subtree作为node的右子树接入
    public func attachAsRC(from node: Node<Element>, subtree: BinaryTree<Element>) -> Node<Element> {
        node.rchild = subtree.root
        subtree.root?.parent = node.rchild
        _size += subtree._size
        updateHeightAbove(node)
        return node
    }
}
